﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument.Subscription
{
    /// <summary>
    /// Listview 的摘要说明。
    /// </summary>
    public partial class SubscriptionListPage : Page
    {
        protected override void OnInit(EventArgs e)
        {
            this.btnDeleteSubscription.Click += new System.EventHandler(this.btnDeleteSubscription_Click);
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bangding();
                //lbl_totalrecord.Text =StaffList.PageCount.ToString();
                //lbl_curpage.Text = txb_PageNo.Text = (StaffList.CurrentPageIndex + 1).ToString();
                //txb_ItemPerPage.Text = StaffList.PageSize.ToString();
                //lbl_totalpage.Text = StaffList.PageCount.ToString();
            }

            base.OnLoad(e);
        }

        private string GetSelectedItemID(string controlID)
        {
            string selectedID = "";
            //遍历DataGrid获得checked的ID
            foreach (DataGridItem item in dgMySubsciption.Items)
            {
                if (((CheckBox) item.FindControl(controlID)).Checked)
                {
                    selectedID += dgMySubsciption.DataKeys[item.ItemIndex] + ",";
                }
            }
            if (selectedID.Length > 0)
            {
                selectedID = selectedID.Substring(0, selectedID.Length - 1);
            }
            return selectedID;
        }

        private void btnDeleteSubscription_Click(object sender, EventArgs e)
        {
            String strIDs = GetSelectedItemID("ClassID");
            if (strIDs == "")
            {
                Response.Write("<script language='javascript'>window.alert('请选择要删除的订阅！');</script>");
            }
            else
            {
                var mySQL = new Database();
                string UserName = SiteContext.Current.UserName;

                SqlParameter[] parameters = {
                    Database.MakeInParam("@UserName", SqlDbType.NVarChar, 300, UserName),
                    Database.MakeInParam("@ClassIDs", SqlDbType.NVarChar, 300, strIDs)
                };

                try
                {
                    mySQL.RunProc("dbo.sproc_DeleteSubsciption", parameters);
                    Response.Write("<script language=javascript>alert('订阅删除成功!');</script>");
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../../../Error.aspx");
                }

                Bangding();
            }
        }

        #region 翻页事件

        public void DataGrid_PageChanged(object sender, DataGridPageChangedEventArgs e)
        {
            dgMySubsciption.CurrentPageIndex = e.NewPageIndex;
            Bangding();
        }

        #endregion

        #region 绑定DBGRID

        private void Bangding()
        {
            var mySQL = new Database();
            string UserName = SiteContext.Current.UserName;

            SqlParameter[] parameters = {
                Database.MakeInParam("@UserName", SqlDbType.NVarChar, 255, UserName)
            };

            DataTable dt;
            using (SqlDataReader dr = mySQL.RunDataReader("dbo.sproc_GetSubscripitionClass", parameters))
            {
                dt = Tools.ConvertDataReaderToDataTable(dr);
            }
            //在DataTable的末尾加上空行，使得DataGrid固定行数
            //			int iBlankRows = dgMySubsciption.PageSize - (dt.Rows.Count % dgMySubsciption.PageSize);
            //			
            //			for (int i = 0; i < iBlankRows; i++)
            //			{
            //				dt.Rows.Add(dt.NewRow()) ;
            //			}

            dgMySubsciption.DataSource = dt.DefaultView;
            dgMySubsciption.DataBind();
            //对于空纪录不显示checkbox
            //
            //			if(dgMySubsciption.CurrentPageIndex  == dgMySubsciption.PageCount -1 )
            //			{
            //				for(int i= (dgMySubsciption.PageSize - iBlankRows)  ;i<dgMySubsciption.Items.Count;i++)
            //				{
            //					dgMySubsciption.Items[i].FindControl("ClassID").Visible = false;
            //				}
            //			}

            btnDeleteSubscription.Attributes["onclick"] = "javascript:return confirm('您确认要删除此订阅吗?');";
        }

        #endregion
    }
}